/*
 to compress...
 d:
 cd TalkTalk\piecesofuk\Javascript
 java -jar D:\Programming\Javascript\yuicompressor-2.3.5\build\yuicompressor-2.3.5.jar singles.js -o singles.min.js
*/

/* TODO update all singles changes including soundmanager library files */

if (typeof PIECESOFUK == "undefined") {
	var PIECESOFUK = {};
}

//soundManager.debugMode = false;
//soundManager.createMovie('../../Javascript/lib/SoundManager2/soundmanager2.swf');
soundManager.url = 'Javascript/soundmanager2.swf'

soundManager.onload = function() {
// soundManager.createSound() etc. may now be called
 soundManager.createSound('WWSYS','Audio/WWSYS.mp3');
 soundManager.createSound('YWMFM','Audio/YWMFM.mp3');
 soundManager.createSound('FG','Audio/FG.mp3');
 soundManager.createSound('MS','Audio/MS.mp3');
 soundManager.createSound('H','Audio/Hands.mp3');
 soundManager.createSound('DSL','Audio/DSL.mp3');
 soundManager.createSound('J','Audio/Jupiter.mp3');
 soundManager.createSound('LU','Audio/LU.mp3');
 soundManager.createSound('WSIT','Audio/WSIT.mp3');
 soundManager.createSound('SS','Audio/SS.mp3');
 soundManager.createSound('BM','Audio/BM.mp3');
 soundManager.createSound('TW','Audio/TW.mp3');
 soundManager.createSound('STE','Audio/STE.mp3');
 soundManager.createSound('I','Audio/Intuition.mp3');
 soundManager.createSound('S','Audio/Stand.mp3');
 soundManager.createSound('2B1','Audio/2Become1.mp3');
 soundManager.createSound('AAA','Audio/AaA.mp3');
 soundManager.createSound('GD','Audio/GoodDay.mp3');
 soundManager.createSound('OOT','Audio/OOT.mp3');
 soundManager.createSound('SW','Audio/SW.mp3');
 soundManager.createSound('ID','Audio/IDo.mp3');
 soundManager.createSound('TIFLC','Audio/TIFLC.mp3');

}


PIECESOFUK.initSingles = function() {
	$("body").show();
	$("#singles").show()
  PIECESOFUK.singlesPtr = new PIECESOFUK.singles();

}


PIECESOFUK.singles = function() {
	var singles = this;
	if ($("#singles").length) {
//		delete this;
//		return; // already exists!
	}
  $("#singles").dialog( {title: "Singles", width: 372, height: 400, close: singles.close} );
	$("#ui-dialog-title-singles")
			   .prepend('<img class="infoViewTitleImg" src="Graphics/singles.png" width="16" height="16" alt="icon">')
         .parent()
				 .css("paddingLeft", "3px");

//  PIECESOFUK.infoViewPtr.update(372, 400, "Singles", this);

//  $("#infoView").html("<div id='singles' /><div id='singlesBar' />").css("width", "340px");
//  $("#infoView").html("<div id='singles' />").css("width", "340px");
//  $("#infoView").html("<div id='singles'/>").css("width", "340px");

  $("#singles").html($("#singlesDescriptionHTML").html());

	var currentAlbum = "POY";

  PIECESOFUK.albums = { "POY": "Pieces Of You", "Spirit": "Spirit",
                      "This Way": "This Way", "0304": "0304",
                      "GBAIW": "Goodbye Alice In Wonderland",
                      "PC": "Perfectly Clear"}

  $.ajax({
    type: "GET",
		async: true,
    url: "Data/singles.js",
    dataType: "json",
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("Error reading Data/singles.js:" + textStatus);
		},
		success: function (data, textStatus) {
			for (var i in data) {
				var $singlesDiv = $("<span class='singles'/>");
//				var $img = $("<img>").attr("src", "Data/" + data[i].img).attr("title", i);
				var $img = $("<img>")
				  .attr( {"src": "Data/" + data[i].img, "title": i })
					.css( {"width": "90px", "opacity": 0.8});
				if ( data[i].album != currentAlbum ) {
					currentAlbum = data[i].album;
					$("<br>").appendTo("#singlesCovers");
				}
				var $albumSpan = $("<span/>").html("<b>" + i + "</b><br><span class='translatable'>Album</span>: " + PIECESOFUK.albums[data[i].album] + "<br>");
				var $writerSpan = $("<span/>").html("<span class='translatable'>written by</span>: " + data[i].writer);
				var $producerSpan = $("<span/>").html("; <span class='translatable'>produced by</span>: " + data[i].producer + "<br>");

				var $videoSpan = $("<span/>").addClass("singleVideo");
				var $vidDirectorSpan = $("<span/>");
				var $vidReleasedSpan = $("<span/>");
				var $vidAwardsSpan = $("<span/>");

				if (data[i].video.available) {
					$vidDirectorSpan.html("<span class='translatable'>Video: directed by</span>: " + data[i].video.director);
					$vidReleasedSpan.html("; <span class='translatable'>released</span>: " + data[i].video.released);
					$vidAwardsSpan.html("; <span class='translatable'>awards</span>: " + data[i].video.awards);
					if (data[i].video.youTube) {
						$videoSpan.addClass(data[i].video.youTube.id);
					}
				} else {
					$videoSpan.text("Video: n/a");
				}

				$videoSpan.append($vidDirectorSpan, $vidReleasedSpan, $vidAwardsSpan, "<br>");

				var $releasesSpan = $("<span/>");
				for (var j = 0; j < data[i].releases.length; j++) {
					var $releaseSpan = $("<span/>");
					var $relCountrySpan = $("<span/>").html("<span class='translatable'>Single</span> (" + data[i].releases[j].country + "): ");
					var $relReleasedSpan = $("<span/>").html("<span class='translatable'>released</span>: " + data[i].releases[j].released);
					var $relPeakSpan = $("<span/>").html("; <span class='translatable'>chart peak</span>: " + data[i].releases[j].peak);
					$relWeeksSpan = $("<span/>").html("; <span class='translatable'>weeks on chart</span>: " + data[i].releases[j].weeks);
					var $relBsidesSpan = $("<span/>");
					if ( data[i].releases[j].bsides != "") {
						$relBsidesSpan.html("; <span class='translatable'>B-sides</span>: " + data[i].releases[j].bSides);
					}
					$releaseSpan.append($relCountrySpan, $relReleasedSpan, $relPeakSpan, $relWeeksSpan, $relBsidesSpan, "<br>");
					$releasesSpan.append($releaseSpan);
				}
				$triviaSpan = $("<span/>");
				if (data[i].trivia != "") {
					$triviaSpan.text("Trivia: " + data[i].trivia).addClass("translatable");
				}
				$singlesDiv.append($img, $albumSpan, $writerSpan, $producerSpan, $videoSpan, $releasesSpan, $triviaSpan);
				$singlesDiv.appendTo("#singlesCovers");
			}
  		$(".singles img").hover( function() {
				$(this).animate( {"opacity": 1.0} );
			}, function() {
				$(this).animate( {"opacity": 0.8} );
			});
  		$(".singles").click( function() {
				soundManager.play($.map( $(this).find("img").attr("title").split(" "), function(s, i) { return s.slice(0, 1) } ).join(""));
				var $displayData = $(this).clone();
				$displayData.find("img").attr("align", "left");
				$("#singles").html($displayData);
				// make sure the view is onscreen when a single is clicked
				var offSet = ( $(window).height() - parseInt( $("#singles").css("height"), 10) ) / 2;
        $("#singles").parent().css("top",  offSet + $(document).scrollTop() + "px" );
			});

			if ( $("#jq-translate-ui").val() != "English") {
        $('#piecesofukMain .translatable').translate( 'english', $("#jq-translate-ui").val(), { //translate from english to the selected language
          not: '.notTranslatable', //exclude these elements
          fromOriginal:true //always translate from english (even after the page has been translated)
      });

			}
		} // success
  }); // $.ajax

  this.close = function() {
		delete singles;
		$("#singles").remove();
		$("#piecesofukMain").html("");
		PIECESOFUK.singlesPtr = null;
  }
} // end PIECESOFUK.singles
